///// ClippingPath /////
using System;
using System.Collections.Generic;
using System.Collections;
//using System.Linq;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;


namespace ClippingPath
{
    class Program
    {
        static void Main(string[] args)
        {
            // Generates a PDF file showing the different canvases in iText.
	        // 
	        // @param args:  no arguments needed here

            Console.WriteLine("Chapter 11: example ClippingPath");
		    Console.WriteLine("-> Creates a PDF file with paths that are constructed");
		    Console.WriteLine("   and used as clipping path.");
		    Console.WriteLine("-> extra resource needed: foxdog.jpg (chapter 5)");
		    Console.WriteLine("-> file generated: template_clip.pdf");
		    // step 1: creation of a document-object
		    Document document = new Document();
		    try 
            {
			    // step 2:
			    // we create a writer
			    PdfWriter writer = PdfWriter.GetInstance(
			            // that listens to the document
					    document,
					    // and directs a PDF-stream to a file
                        new FileStream("clipping_path.pdf", FileMode.Create));
			    // step 3: we open the document
			    document.Open();
			    // step 4:
                Image img = Image.GetInstance(@"D:\Temp\DD.jpg");
			    float w = img.ScaledWidth;
			    float h = img.ScaledHeight;
			    PdfContentByte cb = writer.DirectContent;
			    cb.SaveState();
			    cb.Circle(260, 700, 70);
			    cb.Clip();
			    cb.NewPath();
			    cb.AddImage(img, w, 0, 0, h, 36, 620);
			    cb.RestoreState();
     
			    PdfTemplate tp1 = cb.CreateTemplate(w, h);
			    img.SetAbsolutePosition(0, 0);
			    tp1.RoundRectangle(0, 0, w, h, 10);
			    tp1.Clip();
			    tp1.NewPath();
			    tp1.AddImage(img);
			    cb.AddTemplate(tp1, 36, 420);
     
			    PdfTemplate tp2 = cb.CreateTemplate(90, 90);
			    tp2.MoveTo(10, 0);
			    tp2.LineTo(80, 60);
			    tp2.LineTo(0, 60);
			    tp2.LineTo(70, 0);
			    tp2.LineTo(40, 90);
			    tp2.ClosePath();
			    tp2.Clip();
			    tp2.NewPath();
			    img.SetAbsolutePosition(-40, -100);
			    tp2.AddImage(img);
			    cb.AddTemplate(tp2, 36, 710);
     
			    PdfTemplate tp3 = cb.CreateTemplate(90, 90);
			    tp3.MoveTo(10, 0);
			    tp3.LineTo(80, 60);
			    tp3.LineTo(0, 60);
			    tp3.LineTo(70, 0);
			    tp3.LineTo(40, 90);
			    tp3.ClosePath();
			    tp3.EoClip();
			    tp3.NewPath();
			    tp3.AddImage(img);
			    cb.AddTemplate(tp3, 36, 620);
		    } 
            
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }

            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
 
		    // step 5: we close the document
		    document.Close();

            Console.ReadLine();
        }
    }
}
